package com.atguigu.gmall.all.controller;


import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.list.feign.ListFeignClient;
import com.atguigu.gmall.model.list.SearchParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping
public class ListController {


    @Autowired
    private ListFeignClient listFeignClient;
    //list
    @GetMapping("/list.html")
    public String list(SearchParam searchParam, Model model) throws Exception{

        Result<Map> result = listFeignClient.list(searchParam);
        model.addAllAttributes(result.getData());

        String urlParam = makeUrlParam(searchParam);
        String trademarkParam = makeTrademarkParam(searchParam);
        List<Map<String,String>> propsParamList = makePropsParamList(searchParam);
        Map<String,String> orderMap = makeOrderMap(searchParam);

        model.addAttribute("searchParam",searchParam);
        model.addAttribute("urlParam",urlParam);
        model.addAttribute("trademarkParam",trademarkParam);
        model.addAttribute("propsParamList",propsParamList);
        model.addAttribute("orderMap",orderMap);

        return "list/index";
    }

    private Map<String, String> makeOrderMap(SearchParam searchParam) {
        Map<String, String> orderMap = new HashMap<>();
        String order = searchParam.getOrder();
        if(!StringUtils.isEmpty(order)){
            String[] o = StringUtils.split(order, ":");
            if(null != o && o.length == 2){
                orderMap.put("type",o[0]);
                orderMap.put("sort",o[1]);
            }
        }else {
            orderMap.put("type","1");
            orderMap.put("sort","asc");
        }
        return orderMap;
    }

    private List<Map<String,String>> makePropsParamList(SearchParam searchParam) {
        List<Map<String,String>> propsParamList = new ArrayList<>();
        String[] props = searchParam.getProps();
        if(null != props && props.length > 0){
            for (String prop : props) {
                String[] p = prop.split(":");
                if(null != p && p.length == 3){
                    Map map = new HashMap();
                    map.put("attrName",p[2]);
                    map.put("attrValue",p[1]);
                    map.put("attrId",p[0]);
                    propsParamList.add(map);
                }
            }
        }
        return propsParamList;

    }

    private String makeTrademarkParam(SearchParam searchParam) {

        String trademark = searchParam.getTrademark();
        if(!StringUtils.isEmpty(trademark)){

            String[] t = StringUtils.split(trademark, ":");
            return "品牌:" + t[1];
        }
        return "";
    }


    private String makeUrlParam(SearchParam searchParam) {
        StringBuilder urlParam = new StringBuilder();
        urlParam.append("list.html?");
        if(!StringUtils.isEmpty(searchParam.getKeyword())){
            urlParam.append("&").append("keyword=").append(searchParam.getKeyword());
        }
        if(!StringUtils.isEmpty(searchParam.getCategory1Id())){
            urlParam.append("&").append("category1Id=").append(searchParam.getCategory1Id());
        }
        if(!StringUtils.isEmpty(searchParam.getCategory2Id())){
            urlParam.append("&").append("category2Id=").append(searchParam.getCategory2Id());
        }
        if(!StringUtils.isEmpty(searchParam.getCategory3Id())){
            urlParam.append("&").append("category3Id=").append(searchParam.getCategory3Id());
        }
        if(!StringUtils.isEmpty(searchParam.getTrademark())){
            urlParam.append("&").append("trademark=").append(searchParam.getTrademark());
        }
        String[] props = searchParam.getProps();
        if(null != props && props.length > 0){
            for (String prop : props) {
                urlParam.append("&").append("props=").append(prop);
            }
        }
        return urlParam.toString();
    }
}
